from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput

class TouchUtils:
    def __init__(self, driver):
        self.driver = driver
    #点按
    def tap(self, x, y):
        finger = PointerInput("touch", "finger")
        actions = ActionBuilder(self.driver, mouse=finger)

        actions.pointer_action.move_to_location(x, y)
        actions.pointer_action.pointer_down()
        actions.pointer_action.pause(0.1)
        actions.pointer_action.pointer_up()

        actions.perform()  # 执行动作链
    #长按，默认2s
    def long_press(self, x, y, duration=2000):
        finger = PointerInput("touch", "finger")
        actions = ActionBuilder(self.driver, mouse=finger)  # mouse 参数就是 pointer input

        actions.pointer_action.move_to_location(x, y)
        actions.pointer_action.pointer_down()
        actions.pointer_action.pause(duration / 1000)  # 秒为单位
        actions.pointer_action.pointer_up()
        actions.perform()
#按下移动抬起  ，，滑动
    def swipe(self, start_x, start_y, end_x, end_y, duration=800):
        finger = PointerInput("touch", "finger")
        actions = ActionBuilder(self.driver, mouse=finger)  # ✅ mouse=finger 是关键

        actions.pointer_action.move_to_location(start_x, start_y)
        actions.pointer_action.pointer_down()
        actions.pointer_action.pause(duration / 1000)  # 毫秒转秒
        actions.pointer_action.move_to_location(end_x, end_y)
        actions.pointer_action.pointer_up()

        actions.perform()